#!/bin/sh

# $Id$

# /etc/rc - master bootup script, invokes php setup
# part of pfSense by Scott Ullrich
# Copyright (C) 2004-2010 Scott Ullrich, All rights reserved.
# originally based on m0n0wall (http://neon1.net/m0n0wall)
# Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
# All rights reserved.

#/bin/stty status '^T'
#/bin/stty susp '^-' intr '^-' quit '^-'

#trap : 2
#trap : 3

<<<<<<< HEAD
=======
# Set our operating platform
PLATFORM=`/bin/cat /etc/platform`

>>>>>>> FETCH_HEAD
HOME=/
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
export HOME PATH

<<<<<<< HEAD
# Set our operating platform
PLATFORM=`/bin/cat /etc/platform`

=======
>>>>>>> FETCH_HEAD
# Set our current version
version=`/bin/cat /etc/version`

# Setup dumpdev/ddb/savecore"
echo "Configuring crash dumps..."
if [ "$PLATFORM" = "pfSense" ]; then
	/etc/rc.dumpon
fi

# Setup ddb on all platforms. On full install it will save the dump, on NanoBSD it will print to console and auto-reboot.
if [ ! -z "`sysctl -Nq debug.ddb.scripting.scripts`" ]; then
	/sbin/ddb /etc/ddb.conf
fi

<<<<<<< HEAD
if [ -e /root/force_fsck ]; then
	echo "Forcing filesystem check..."
	/sbin/fsck -y -t ufs /
	if [ "$PLATFORM" = "nanobsd" ]; then
		/sbin/fsck -y -t ufs /cf
	fi
fi

if [ "${PLATFORM}" = "nanobsd" ]; then
	kldstat -qm zfs
	if [ $? = 0 ]; then
		kldunload zfs
	fi
fi
=======
if [ "$PLATFORM" = "nanobsd" ]; then
	/etc/rc.embedded
fi

if [ "$PLATFORM" = "pfSense" ]; then
	mdmfs -S -M -s 4m md /var/run
fi

product=`/bin/cat /etc/inc/globals.inc | /usr/bin/grep product_name | /usr/bin/cut -d'"' -f4`
hideplatform=`cat /etc/inc/globals.inc | grep hideplatform | wc -l`

if [ "$hideplatform" -gt "0" ]; then
	platformbanner="" # hide the platform
else
	platformbanner=" on the '${PLATFORM}' platform"
fi

echo
cat /etc/ascii-art/pfsense-logo-small.txt
echo
echo
echo "Welcome to ${product} ${version}${platformbanner}..."
echo

# Enable console output if its muted.
/sbin/conscontrol mute off >/dev/null
>>>>>>> FETCH_HEAD

# Mount memory file system if it exists
echo "Mounting filesystems..."

kldstat -qm zfs
# Handle ZFS read-only case
if [ "$PLATFORM" = "pfSense" ]; then
	kldstat -qm zfs
	if [ $? = 0 ]; then
		ZFSFSAVAILABLE=$(/sbin/zfs mount 2>/dev/null | wc -l)
		if [ $ZFSAVAILABLE -eq 0 ]; then
			kldunload zfs
		elif [ -f /usr/bin/grep ]; then
			ZFSROOT=`/sbin/zfs mount | /usr/bin/grep ' /$' | /usr/bin/cut -d ' ' -f 1`
			if [ "$ZFSROOT" != "" ]; then
				/sbin/zfs set readonly=off $ZFSROOT
			fi
		fi
	fi
fi

if [ "${PLATFORM}" = "cdrom" ]; then
	/etc/rc.cdrom
else
	# Mount /. If it fails run a fsck.
	if [ "$PLATFORM" = "nanobsd" ]; then
		export PKG_TMPDIR=/root/
		/sbin/mount -uw / 2>/dev/null
		mount_rc=$?
		attempts=0
		while [ ${mount_rc} != 0 -a ${attempts} -lt 3 ]; do
			/sbin/fsck -y /
			/sbin/fsck -y /cf
			/sbin/mount -uw / 2>/dev/null
			mount_rc=$?
			attempts=$((attempts+1))
		done
	else
		/sbin/mount -a 2>/dev/null
		mount_rc=$?
		attempts=0
		while [ ${mount_rc} != 0 -a ${attempts} -lt 3 ]; do
			/sbin/fsck -y /
			/sbin/mount -a 2>/dev/null
			mount_rc=$?
			attempts=$((attempts+1))
		done
	fi

	# If /conf is a directory, convert it to a symlink to /cf/conf
	if [ -d "/conf" ]; then
		# If item is not a symlink then rm and recreate
		CONFPOINTSTO=`readlink /conf` 
		if ! test "x$CONFPOINTSTO" = "x/cf/conf"; then 
<<<<<<< HEAD
			/bin/rm -rf /conf
			/bin/ln -s /cf/conf /conf
		fi
	fi

	if [ ! "$PLATFORM" = "jail" ]; then
		# Check to see if a compact flash mountpoint exists
		# If it fails to mount then run a fsck -y
		if grep -q cf /etc/fstab; then
			/sbin/mount -w /cf 2>/dev/null
			/sbin/mount -uw /cf 2>/dev/null
			mount_rc=$?
			attempts=0
			while [ ${mount_rc} != 0 -a ${attempts} -lt 3 ]; do
				/sbin/umount /cf
				/sbin/fsck -y /cf
				/sbin/mount -w /cf 2>/dev/null
				mount_rc=$?
				attempts=$((attempts+1))
			done
=======
			rm -rf /conf
			ln -s /cf/conf /conf
>>>>>>> FETCH_HEAD
		fi
	fi

	USE_MFS_TMPVAR=`/usr/bin/grep -c use_mfs_tmpvar /cf/conf/config.xml`
	if [ "${PLATFORM}" = "nanobsd" ] || [ ${USE_MFS_TMPVAR} -gt 0 ]; then
		/etc/rc.embedded
	fi
fi

/bin/rm -f /root/force_fsck
/bin/rm -f /root/TRIM_set
/bin/rm -f /root/TRIM_unset

# Disable APM on ATA drives. Leaving this on will kill drives long-term, especially laptop drives, by generating excessive Load Cycles.
if [ -f /etc/rc.disable_hdd_apm ]; then
	/etc/rc.disable_hdd_apm
fi

#Eject CD devices on 3G modems
MANUFACTURER="huawei|zte"
CDDEVICE=`dmesg |egrep -ie "($MANUFACTURER)" | awk -F: '/cd/ {print $1}'`
if [ "$CDDEVICE" != "" ]; then
	cdcontrol -f /dev/"$CDDEVICE" eject
fi

# sync pw database after mount.
rm -f /etc/spwd.db.tmp
/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd

product=`/usr/bin/grep product_name /etc/inc/globals.inc | /usr/bin/cut -d'"' -f4`
hideplatform=`/usr/bin/grep hideplatform /etc/inc/globals.inc | /usr/bin/wc -l`
varrunpath=`/usr/bin/grep varrun_path /etc/inc/globals.inc | /usr/bin/cut -d'"' -f4`

if [ "$PLATFORM" = "pfSense" ] && [ ${USE_MFS_TMPVAR} -eq 0 ]; then
	/sbin/mdmfs -S -M -s 4m md $varrunpath
fi

if [ "$hideplatform" -gt "0" ]; then
	platformbanner="" # hide the platform
else
	platformbanner=" on the '${PLATFORM}' platform"
fi

echo
cat /etc/ascii-art/pfsense-logo-small.txt
echo
echo
echo "Welcome to ${product} ${version} ${platformbanner} ..."
echo

if [ ! "$PLATFORM" = "jail" ]; then
	# Enable console output if its muted.
	/sbin/conscontrol mute off >/dev/null
fi

if [ "$PLATFORM" = "cdrom" ] ; then
	# do nothing for cdrom platform
<<<<<<< HEAD
elif [ "$PLATFORM" = "nanobsd" ] || [ ${USE_MFS_TMPVAR} -gt 0 ]; then
	# Ensure that old-style PKG packages can be persistent across reboots
	/bin/mkdir -p /root/var/db/pkg
	/bin/rm -rf /var/db/pkg
	/bin/ln -s /root/var/db/pkg/ /var/db/pkg
	# Ensure that PBI packages can be persistent across reboots
	/bin/mkdir -p /root/var/db/pbi
	/bin/rm -rf /var/db/pbi
	/bin/ln -s /root/var/db/pbi/ /var/db/pbi
elif [ "$PLATFORM" = "jail" ]; then
	# do nothing for jail platform
=======
elif [ "$PLATFORM" = "embedded" ] ; then
    # do nothing for embedded platform
elif [ "$PLATFORM" = "nanobsd" ] ; then
	# Ensure that packages can be persistent across reboots
	/bin/mkdir -p /root/var/db/pkg
	rm -rf /var/db/pkg
	ln -s /root/var/db/pkg/ /var/db/pkg
>>>>>>> FETCH_HEAD
else
	SWAPDEVICE=`/bin/cat /etc/fstab | /usr/bin/grep swap | /usr/bin/cut -f1`
	/sbin/swapon -a 2>/dev/null >/dev/null
	/etc/rc.savecore

	if [ -d /root/var/db/pkg ]; then
		# User must have just disabled RAM disks, move these back into place.
		/bin/mkdir -p /var/db/pkg
		/bin/mv /root/var/db/pkg /var/db/pkg
		/bin/mkdir -p /var/db/pbi
		/bin/mv /root/var/db/pbi /var/db/pbi
	fi
fi

# Copy PBI keys
if ls /usr/local/share/pbi-keys/*.ssl >/dev/null 2>&1; then
	if [ ! -d "/var/db/pbi/keys" ]; then
		mkdir -p /var/db/pbi/keys
	fi
	cp -f /usr/local/share/pbi-keys/*.ssl /var/db/pbi/keys
fi

if [ "$PLATFORM" = "cdrom" ] ; then
	echo -n "Mounting unionfs directories..."
	/bin/mkdir /tmp/unionfs
	/bin/mkdir /tmp/unionfs/usr
	/bin/mkdir /tmp/unionfs/root
	/bin/mkdir /tmp/unionfs/sbin
	/bin/mkdir /tmp/unionfs/bin
	/bin/mkdir /tmp/unionfs/boot
	/bin/mkdir /tmp/unionfs/confdefault
	/sbin/mount_unionfs /tmp/unionfs/usr /usr/
	/sbin/mount_unionfs /tmp/unionfs/root /root/
	/sbin/mount_unionfs /tmp/unionfs/bin /bin/		
	/sbin/mount_unionfs /tmp/unionfs/sbin /sbin/	
	/sbin/mount_unionfs /tmp/unionfs/boot /boot/
	/sbin/mount_unionfs /tmp/unionfs/confdefault /conf.default/
	echo "done."
fi

# make some directories in /var
/bin/mkdir -p $varrunpath /var/log /var/etc /var/db/entropy /var/db/rrd /var/at/jobs/ /var/empty 2>/dev/null
/bin/rm -rf $varrunpath/*
if [ "$PLATFORM" != "pfSense" ]; then
	/bin/rm /var/log/* 2>/dev/null
fi

# Cleanup configuration files from previous instance
/bin/rm -rf /var/etc/*
/bin/rm -rf /var/tmp/*

echo -n "Creating symlinks..."
# Make sure symlink is correct on nanobsd
if [ "$PLATFORM" = "nanobsd" ] ; then
	/bin/rm /conf
	/bin/ln -s /cf/conf/ /conf
fi

# Make sure symlink is correct on embedded
if [ "$PLATFORM" = "nanobsd" ] ; then
	rm /conf
	ln -s /cf/conf/ /conf
fi

# Repair symlinks if they are broken
if [ -f /etc/newsyslog.conf ]; then
	/bin/rm -f /etc/newsyslog.conf
fi
if [ ! -L /etc/syslog.conf ]; then
	/bin/rm -rf /etc/syslog.conf
	if [ ! -f /var/etc/syslog.conf ]; then
		touch /var/etc/syslog.conf
	fi
	/bin/ln -s /var/etc/syslog.conf /etc/syslog.conf
fi

# Repair symlinks if they are broken
if [ ! -L /etc/hosts ]; then
	/bin/rm -rf /etc/hosts
	/bin/ln -s /var/etc/hosts /etc/hosts
fi

if [ ! -L /etc/resolv.conf ]; then
    /bin/rm -rf /etc/resolv.conf
    /bin/ln -s /var/etc/resolv.conf /etc/resolv.conf
fi

<<<<<<< HEAD
if [ ! -L /etc/resolvconf.conf ]; then
    /bin/rm -rf /etc/resolvconf.conf
    /bin/ln -s /var/etc/resolvconf.conf /etc/resolvconf.conf
=======
# Setup compatibility link for packages that
# have trouble overriding the PREFIX configure
# argument since we build our packages in a
# seperated PREFIX area
# Only create if symlink does not exist. 
if [ ! -h /tmp/tmp ]; then
    ln -hfs / /tmp/tmp
fi

# Make sure our /tmp is 777 + Sticky
chmod 1777 /tmp

# Malloc debugging check
if [ ! -L /etc/malloc.conf ]; then
    ln -s aj /etc/malloc.conf
>>>>>>> FETCH_HEAD
fi

# Setup compatibility link for packages that
# have trouble overriding the PREFIX configure
# argument since we build our packages in a
# separated PREFIX area
# Only create if symlink does not exist. 
if [ ! -h /tmp/tmp ]; then
    /bin/ln -hfs / /tmp/tmp
fi

# Make sure our /tmp is 777 + Sticky
if [ ! "$PLATFORM" = "cdrom" ] ; then
	/bin/rm -rf /tmp/*
fi
/bin/chmod 1777 /tmp

if [ ! "$PLATFORM" = "cdrom" ] ; then
	# Malloc debugging check
	if [ -L /etc/malloc.conf ]; then
	    #ln -s aj /etc/malloc.conf
		/bin/rm /etc/malloc.conf
	fi
fi

if [ ! -L /etc/dhclient.conf ]; then
    /bin/rm -rf /etc/dhclient.conf
fi

if [ ! -d /var/tmp ]; then
	/bin/mkdir -p /var/tmp
fi

if [ ! -d /cf/conf/backup/ ]; then
	/bin/mkdir -p /cf/conf/backup/ 
fi

set -T
trap "echo 'Reboot interrupted'; exit 1" 3

# Remove old nameserver resolution files
/bin/rm -f /var/etc/nameserver*

# Create uploadbar tmp directory
/bin/mkdir -p /tmp/uploadbar
/bin/chmod 0777 /tmp/uploadbar

echo -n "."
<<<<<<< HEAD
DISABLESYSLOGCLOG=`/usr/bin/grep -c disablesyslogclog /cf/conf/config.xml`
ENABLEFIFOLOG=`/usr/bin/grep -c usefifolog /cf/conf/config.xml`
LOG_FILES="system filter dhcpd vpn pptps poes l2tps openvpn portalauth ipsec ppp relayd wireless lighttpd ntpd gateways resolver routing"

DEFAULT_LOG_FILE_SIZE=`/usr/local/bin/xmllint --xpath 'string(//pfsense/syslog/logfilesize)' /conf/config.xml`
if [ ! ${DEFAULT_LOG_FILE_SIZE} ]; then
	DEFAULT_LOG_FILE_SIZE=511488
=======
# generate circular logfiles
if [ ! "$PLATFORM" = "cdrom" ]; then
	clog -i -s 512144 /var/log/system.log
	clog -i -s 512144 /var/log/filter.log
	clog -i -s 65535 /var/log/dhcpd.log
	clog -i -s 65535 /var/log/vpn.log
	clog -i -s 65535 /var/log/openvpn.log
	clog -i -s 65535 /var/log/portalauth.log
	clog -i -s 65535 /var/log/ipsec.log
	clog -i -s 65535 /var/log/slbd.log
	clog -i -s 65535 /var/log/lighttpd.log
	clog -i -s 65535 /var/log/ntpd.log
	clog -i -s 65535 /var/log/apinger.log
else
    clog -i -s 65535 /var/log/system.log
    clog -i -s 65535 /var/log/filter.log
    clog -i -s 65535 /var/log/dhcpd.log
    clog -i -s 65535 /var/log/vpn.log
	clog -i -s 65535 /var/log/openvpn.log
    clog -i -s 65535 /var/log/portalauth.log
    clog -i -s 65535 /var/log/ipsec.log
    clog -i -s 65535 /var/log/slbd.log
	clog -i -s 65535 /var/log/ntpd.log
	clog -i -s 65535 /var/log/apinger.log
>>>>>>> FETCH_HEAD
fi

for logfile in $LOG_FILES; do
	if [ "$DISABLESYSLOGCLOG" -gt "0" ]; then 
		/usr/bin/touch /var/log/$logfile.log
	else 
		if [ ! -f /var/log/$logfile.log ]; then
			if [ "$ENABLEFIFOLOG" -gt "0" ]; then
				# generate fifolog files
				/usr/sbin/fifolog_create -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
			else 
				/usr/local/sbin/clog -i -s ${DEFAULT_LOG_FILE_SIZE} /var/log/$logfile.log
			fi
		fi
	fi 
done

# change permissions on newly created fifolog files.
/bin/chmod 0600 /var/log/*.log

echo -n "."
if [ ! "$PLATFORM" = "jail" ]; then
	DEVFS=`/sbin/mount | /usr/bin/grep devfs | /usr/bin/wc -l | /usr/bin/cut -d" " -f8`
	if [ "$DEVFS" = "0" ]; then
		mount_devfs devfs /dev
	fi
fi

# Create an initial utmp file
cd $varrunpath && /bin/cp /dev/null utmp && /bin/chmod 644 utmp

echo -n "."
/sbin/ldconfig -elf /usr/lib /usr/local/lib /lib
/etc/rc.d/ldconfig start 2>/dev/null

# Make sure /etc/rc.conf doesn't exist.
if [ -f /etc/rc.conf ]; then
    /bin/rm -rf /etc/rc.conf
fi

if [ ! "$PLATFORM" = "jail" ]; then
	# Launching kbdmux(4)
	if [ -f "/dev/kbdmux0" ]; then
		echo -n "."
		/usr/sbin/kbdcontrol -k /dev/kbdmux0 < /dev/console
		[ -c "/dev/atkbd0" ] && kbdcontrol -a atkbd0 < /dev/console
		[ -c "/dev/ukbd0" ] && kbdcontrol -a ukbd0 < /dev/console
	fi

	# Fire up unionfs if mount points exist.
	if [ -f /dist/uniondirs ]; then
		echo -n "."
		/etc/rc.d/unionfs start
	fi
fi

echo "done."

# Recreate capabilities DB
/usr/bin/cap_mkdb /etc/login.conf

# Run the php.ini setup file and populate
# /usr/local/etc/php.ini and /usr/local/lib/php.ini
/etc/rc.php_ini_setup 2>/tmp/php_errors.txt
/usr/local/sbin/php-fpm -c /usr/local/lib/php.ini -y /usr/local/lib/php-fpm.conf -RD 2>&1 >/dev/null

# Launch external configuration loader for supported platforms
if [ "$PLATFORM" = "nanobsd" ]; then
	/usr/local/sbin/fcgicli -f /etc/ecl.php
fi

# Launch external configuration loader for supported platforms
if [ "$PLATFORM" = "pfSense" ]; then
	/usr/local/sbin/fcgicli -f /etc/ecl.php
fi

if [ -f /etc/rc.custom_boot_early ]; then
	/bin/echo -n "Launching /etc/rc.custom_boot_early...";
	/etc/rc.custom_boot_early
	echo "Done"
fi

export fcgipath=/var/run/php-fpm.socket
/usr/bin/nice -n20 /usr/local/sbin/check_reload_status

if [ "$PLATFORM" = "nanobsd" -o "$PLATFORM" = "embedded" ]; then
	upload_tmp_dir="/root"
else
	upload_tmp_dir="/tmp"
fi

if [ "$PLATFORM" != "cdrom" ]; then
# Populate a dummy php.ini to avoid
# the file being clobbered and the firewall
# not being able to boot back up.
cat >/usr/local/lib/php.ini <<EOF
output_buffering = "0"
expose_php = Off
implicit_flush = true
magic_quotes_gpc = Off
max_execution_time = 99999999
max_input_time = 99999999
register_argc_argv = On
file_uploads = On
upload_tmp_dir = ${upload_tmp_dir}
upload_max_filesize = 90M
post_max_size = 90M
html_errors = Off
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg"
apc.enabled="1"
apc.enable_cli="1"
apc.shm_size="30"
extension_dir=/usr/local/lib/php/extensions/no-debug-non-zts-20020429/
extension=apc.so
extension=bcmath.so
extension=ctype.so
extension=curl.so
extension=mbstring.so
extension=overload.so
extension=pcntl.so
extension=pcre.so
extension=posix.so
extension=radius.so
extension=readline.so
extension=session.so
extension=sysvsem.so
extension=tokenizer.so
extension=xml.so

EOF
fi

rm -f /cf/conf/backup/backup.cache

# Copy php.ini to alternate location after generation
cp /usr/local/lib/php.ini /usr/local/etc/php.ini

# let the PHP-based configuration subsystem set up the system now
echo -n "Launching the init system..."
<<<<<<< HEAD
/bin/rm -f /cf/conf/backup/backup.cache
/bin/rm -f /root/lighttpd*
/usr/bin/touch $varrunpath/booting
/etc/rc.bootup

# /etc/rc.bootup unset $g['booting'], and removes file
# Be sure the file is removed to not create troubles after
if [ -f $varrunpath/booting ]; then
	/bin/rm $varrunpath/booting
fi

=======
/etc/rc.bootup

>>>>>>> FETCH_HEAD
# If a shell was selected from recovery 
# console then just drop to the shell now.
if [ -f "/tmp/donotbootup" ]; then
	echo "Dropping to recovery shell."
	exit 0
fi

echo -n "Starting CRON... "
cd /tmp && /usr/sbin/cron -s 2>/dev/null
echo "done."

# Start packages
/usr/local/sbin/fcgicli -f /etc/rc.start_packages

/bin/rm -rf /usr/local/pkg/pf/CVS

# Start ping handler every 240 seconds
/usr/local/bin/minicron 240 $varrunpath/ping_hosts.pid /usr/local/bin/ping_hosts.sh

# Start account expire handler every hour
/usr/local/bin/minicron 3600 $varrunpath/expire_accounts.pid '/usr/local/sbin/fcgicli -f /etc/rc.expireaccounts'

<<<<<<< HEAD
# Start alias url updater every 24 hours
/usr/local/bin/minicron 86400 $varrunpath/update_alias_url_data.pid '/usr/local/sbin/fcgicli -f /etc/rc.update_alias_url_data'

/bin/chmod a+rw /tmp/.

# Check for GEOM mirrors
GMIRROR_STATUS=`/sbin/gmirror status`
if [ "${GMIRROR_STATUS}" != "" ]; then
	# Using a flag file at bootup saves an expensive exec/check on each page load.
	/usr/bin/touch /var/run/gmirror_active
	# Setup monitoring/notifications
	/usr/local/bin/minicron 60 /var/run/gmirror_status_check.pid /usr/local/sbin/gmirror_status_check.php
fi

# Log product version to syslog
BUILDTIME=`cat /etc/version.buildtime`
ARCH=`uname -m`
echo "$product ($PLATFORM) $version $ARCH $BUILDTIME"

echo "Bootup complete"
=======
# Remove stale files that have already been processed by bootup
# scripts
rm -f /tmp/filter_dirty
rm -f /tmp/rc.linkup
nohup /usr/bin/nice -n20 /usr/local/sbin/check_reload_status &

# Start ping handler for every 240 seconds
minicron 240 /var/run/ping_hosts.pid /usr/local/bin/ping_hosts.sh
>>>>>>> FETCH_HEAD

/usr/local/bin/beep.sh start 2>&1 >/dev/null

# Reset the cache.  read-only requires this.
<<<<<<< HEAD
/bin/rm -f /tmp/config.cache
=======
rm /tmp/config.cache

/etc/rc.conf_mount_ro

echo "Bootup complete"
>>>>>>> FETCH_HEAD

exit 0
